package com.qilu.pe.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qilu.pe.R;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Config;
import com.qilu.pe.dao.bean.SetType;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.GlideUtil;
import com.qilu.pe.support.view.GridItemDecoration;
import com.qilu.pe.ui.activity.CrowdListActivity;
import com.qilu.pe.ui.activity.SetListActivity;
import com.qilu.pe.ui.activity.SickTypeActivity;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

// ai体检
public class Home3Fragment extends BaseFragment {
    private RecyclerView rcv_set_type_list;

    private RelativeLayout rl_sick_query, rl_life_habit;// 疾病查询 生活习惯

    private List<SetType> typeList = new ArrayList<>();
    private SetTypeListAdapter adapter;

    public Home3Fragment() {

    }

    public static Home3Fragment newInstance() {
        Home3Fragment fragment = new Home3Fragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new SetTypeListAdapter(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_home3, container, false);
        rl_life_habit = v.findViewById(R.id.rl_life_habit);
        rl_sick_query = v.findViewById(R.id.rl_sick_query);
        rcv_set_type_list = v.findViewById(R.id.rcv_set_type_list);

        GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);
        GridItemDecoration divider = new GridItemDecoration.Builder(getActivity())
                .setHorizontalSpan(R.dimen.deco_1_dp)
                .setVerticalSpan(R.dimen.deco_1_dp)
                .setColorResource(R.color.divider_color)
                .setShowLastLine(true)
                .build();
        rcv_set_type_list.addItemDecoration(divider);
        rcv_set_type_list.setLayoutManager(manager);
        rcv_set_type_list.setAdapter(adapter);

        setListeners();

        hideProgress();
        reqSetTypeList();
        showProgress();

        return v;
    }

    private void setListeners() {
        setOnClickListener(rl_sick_query, rl_life_habit);
        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("setType", typeList.get(position));
                startActivity(SetListActivity.class, bundle);
            }
        });
    }

    // 请求套餐分类列表
    private Disposable reqSetTypeList() {
        return APIRetrofit.getDefault().reqSetTypeList("")
                .compose(RxUtil.<BaseResult2<List<SetType>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<SetType>>>() {
                    @Override
                    public void accept(BaseResult2<List<SetType>> listBaseResult2) throws Exception {
                        hideProgress();
                        if (listBaseResult2.isSuccess()) {
                            typeList = listBaseResult2.getData();
                            adapter.setDataList(typeList);
                        } else {

                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }

    @Override
    public void onClick(View v) {
        if (v == rl_life_habit) {
            startActivity(CrowdListActivity.class);
        }
        if (v == rl_sick_query) {
            startActivity(SickTypeActivity.class);
        }
    }

    private class SetTypeListAdapter extends ListBaseAdapter<SetType> {

        public SetTypeListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_set_type_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            ImageView ic_13 = holder.getView(R.id.ic_13);
            TextView tv_type_name = holder.getView(R.id.tv_type_name);

            SetType type = mDataList.get(position);
            tv_type_name.setText(type.getName());
            GlideUtil.loadImg(Config.URL_IMG + type.getPicture(), ic_13);
        }
    }
}